# -*- coding: utf-8 -*-
from fastapi import APIRouter, HTTPException,Depends

from loguru import logger

from app.api import jwt
from app.common.response.response_schema import response_base
from app.api.service import task_service
from app.crud import crud_task
from app.schemas.task import RQTask,RQRunTask, RQTaskSleepTime, RQUpdateTask
from app.models.user import User

# 创建status router路由
task_router = APIRouter()


# 查询放在后端里面的任务链
@task_router.get("/")
async def get_task(current_user: User = Depends(jwt.get_current_user)):
    tasks = await task_service.get_tasks(current_user)
    return response_base.response_200(msg='加载成功',data=tasks)


# 任务链设置
@task_router.post("/")
async def create_task(params:RQTask, current_user: User = Depends(jwt.get_current_is_agv_user)):
    await task_service.create_task(params)
    return response_base.response_200(msg='创建成功')

@task_router.put("/sleep")
async def update_task_sleep_time(params:RQTaskSleepTime, current_user: User = Depends(jwt.get_current_is_agv_user)):
    await task_service.update_task_sleep_time(params)
    return response_base.response_200(msg='更新成功')



@task_router.post("/run",)
async def create_task(params:RQRunTask,current_user: User = Depends(jwt.get_current_is_agv_user)):
    await task_service.run_task(params)
    return response_base.response_200(msg='运行成功')
    
    
@task_router.delete('/')
async def updata_task(task_name:str,current_user: User = Depends(jwt.get_current_is_agv_user)):
    await task_service.del_task(task_name)
    return response_base.response_200(msg='删除成功')


@task_router.put('/')
async def put_task(obj:RQUpdateTask,current_user: User = Depends(jwt.get_current_is_agv_user)):
    res = await task_service.update_task(obj)
    return response_base.response_200(msg='更新成功',data=res)






